from kernel.core import Spider, Monitor
from kernel.utils import Logger


class BaiduSpider(Spider):  # 继承内核类 Spider
    logger = Logger(category='Spider', class_name='Baidu').get_logger()
    name = '百度'
    domain = 'https://www.baidu.com'

    def __init__(self, config=None, monitor=Monitor()):
        Spider.__init__(self, config, monitor)

    def download(self, page):
        """
        重写父类方法download，方法体可自行开发，也可用系统提供的Downloader工具类，
        :param page:
        :return:
        """
        url = page.url
        response = self.downloader.get(url)
        if response.status_code == 200:
            self.logger.info(response.content.title())
            page.result = {
                'success': True
            }
            page.status = True


if __name__ == '__main__':
    jsonConfig = {
        'spiderId': 1,
        'state': 1,
        "website": "",
        "domain": "",
        "spiderQueue": "CRAWLER-DEV-UQ-Baidu",
        "reaperQueue": "CRAWLER-DEV-RQ-Baidu",
        "siteName": "百度",
        'threadNum': 1
    }
    spider = BaiduSpider(jsonConfig)
    spider.start_url("https://www.baidu.com")
    spider.start()
